package chapter03
import org.apache.spark.{SparkConf, SparkContext}
object Test07_MapPartitions {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf().setMaster("local[*]").setAppName("MapPartitions")
    val sc = new SparkContext(conf)
    //创建一个分区的rdd
    val value = sc.makeRDD(List(1, 2, 3, 4, 5, 6, 7, 8, 9),3)
    println(value.max)
    //过滤所有能被2整除的元素
    val value1 = value.mapPartitions(e => {
      e.filter(f => f % 2 == 0)
    })
    println(value1.collect().mkString("Array(", ", ", ")"))
    //将每个元素上加1
    val value2 = value.mapPartitions(e => {
      e.map(f => f + 1)
    })
    println(value2.collect().mkString("Array(", ", ", ")"))
    //每个分区的值
    value.foreachPartition(e=>println(e.toList))
    //计算每个分区的最大值
    val value3 = value.mapPartitions(e => Iterator(e.max))
    println(value3.collect().mkString("Array(", ", ", ")"))
  }
}
